﻿package com.neriksworkshop.lib.colourLovers.items
{
	import com.neriksworkshop.lib.colourLovers.core.AbstractGraphicItem;
	import com.neriksworkshop.lib.colourLovers.params.Params;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Color extends AbstractGraphicItem implements IGraphicItem
	{
		
		//from colourlovers api
		private var _hexStr:String;
		private var _rgb:Array;
		private var _hsv:Array;		
		
		//computed
		private var _hex:Number;
		

		
		public function Color(value:Number = NaN) 
		{
			super(this);
			if (!isNaN(value))
			{
				hex = value;
			}
		}
		
		
		override public function buildFromData(data:*):void
		{
			var dataFinal:* = (data is String) ? XML(data) : data;
			
			super.buildFromData(dataFinal);
			
			_hexStr = dataFinal.hex;
			_rgb = [Number('0x'+dataFinal.rgb.red), Number('0x'+dataFinal.rgb.green), Number('0x'+dataFinal.rgb.blue)];
			_hsv = [Number('0x'+dataFinal.hsv.hue), Number('0x'+dataFinal.hsv.saturation), Number('0x'+dataFinal.hsv.value)];
			_hex = Number('0x' + _hexStr);
		}
		
			/*
		override public function buildFromXML(xmlData:XML):void 
		{
			super.buildFromXML(xmlData);
			_hexStr = xmlData.hex;
			_rgb = [Number('0x'+xmlData.rgb.red), Number('0x'+xmlData.rgb.green), Number('0x'+xmlData.rgb.blue)];
			_hsv = [Number('0x'+xmlData.hsv.hue), Number('0x'+xmlData.hsv.saturation), Number('0x'+xmlData.hsv.value)];
			_hex = Number('0x' + _hexStr);
			
		}
		
		public override function buildFromJSON(jsonData:Object):void
		{
			super.buildFromJSON(jsonData);
			_hexStr = jsonData.hex;
			_rgb = [Number('0x'+jsonData.rgb.red), Number('0x'+jsonData.rgb.green), Number('0x'+jsonData.rgb.blue)];
			_hsv = [Number('0x'+jsonData.hsv.hue), Number('0x'+jsonData.hsv.saturation), Number('0x'+jsonData.hsv.value)];
			_hex = Number('0x' + _hexStr);
		}*/
		
		
		public override function createSprite(dimensions:Rectangle = null):Sprite
		{
			if (!dimensions) var dimensions:Rectangle = Params.DEFAULT_DIMENSIONS;
			
			_sp = new Sprite();
			_sp.graphics.beginFill(_hex);
			_sp.graphics.drawRect(dimensions.x, dimensions.y, dimensions.width, dimensions.height);
			return _sp;
		}
		

		public function get hexStr():String { return _hexStr; }
		public function get rgb():Array { return _rgb; }
		//TODO HSV convert
		//public function get hsv():Array { return _hsv; }
		public function get hex():Number { return _hex; }
		public function get r():Number { return _rgb[0] };
		public function get g():Number { return _rgb[1] };
		public function get b():Number { return _rgb[2] };
		
		
		
		public function set hexStr(value:String):void 
		{
			_hexStr = value;
			computeFromHexStr();
		}
			
		
		public function set rgb(value:Array):void 
		{
			_rgb = value;
			computeFromRgb();
		}
				
		public function set hex(value:Number):void 
		{
			_hex = value;
			computeFromHex();
		}
		
		
		
		
		//--------------------------------private------------------------------
		private function computeFromHexStr():void
		{
			_hex = Number('0x' + _hexStr);
			_rgb = hexToRgb(_hex);
		}		
		
		private function computeFromHex():void
		{
			_rgb = hexToRgb(_hex);
			_hexStr = hexToHexStr(_hex);
		}
				
		private function computeFromRgb():void
		{
			
		}		
		
		
	

		//--------------------------------static-------------------------
		public static function hexToRgb(hexValue:Number):Array
		{
			var r:Number = hexValue >> 16;
			var g:Number = hexValue >> 8 & 0xFF;
			var b:Number = hexValue & 0xFF;
			return [r, g, b];
		}
		
		public static function rgbToHex(rgbValue:Array):Number
		{
			return (rgbValue[0] << 16 | rgbValue[1] << 8 | rgbValue[2]);
		}
		
		public static function hexToHexStr(hexValue:Number):String
		{
			return hexValue.toString(16);
		}
		
		public static function rgbToHsv(rgbValue:Array):Array
		{
			var r:int = rgbValue[0] / 0xFF;
			var g:int = rgbValue[1] / 0xFF;
			var b:int = rgbValue[2] / 0xFF;
			return [];
			
		}
		
		
	}
	
	
	
}